﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DblogStudio.BLL;
using DblogStudio.DAL.Models;
using DblogStudio.GUI.Utils;

namespace DblogStudio.GUI.DuLieu
{
    public partial class FormQuanLyMaySanXuat : DblogStudio.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        public FormQuanLyMaySanXuat()
        {
            InitializeComponent();
            gridData.AddRowIndexing();
            allowUpdate = false;
            InitGrid();
            if (LoadCongDoanSanXuat())
                LoadGrid();
            allowUpdate = true;
        }

        private bool LoadCongDoanSanXuat()
        {
            var ls = BizCongDoanSanXuat.GetList();
            cmbCongDoanSanXuat.DataSource = ls;
            cmbCongDoanSanXuat.DisplayMember = "TenCongDoanSanXuat";
            cmbCongDoanSanXuat.ValueMember = "MaCongDoanSanXuat";
            if (ls != null && ls.Count > 0)
            {
                btnThem.Enabled = true;
                return true;
            }
            else
            {
                btnThem.Enabled = false;
                return false;
            }
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
        }

        void LoadGrid()
        {
            if (btnThem.Enabled)
            {
                bsMaySanXuat.DataSource = BizMaySanXuat.GetList(cmbCongDoanSanXuat.SelectedValue.ToString());
                gridData.DataSource = bsMaySanXuat;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var current = bsMaySanXuat.Current as MaySanXuat;
            if (current != null)
            {
                if (DialogUtils.ConfirmedWithTitle("Chú ý", "Bạn có chắc chắn muốn xoá máy này khỏi hệ thống không?"))
                {
                    try
                    {
                        var rs = BizMaySanXuat.Delete(current.MaMaySanXuat);
                        LoadGrid();
                    }
                    catch (Exception exc)
                    {
                        DialogUtils.ShowErrorWithDefaultTitle("Không thể xoá máy này.\n" + exc.Message);
                    }
                }
            }
        }

        private void bdData_CurrentChanged(object sender, EventArgs e)
        {
            var current = bsMaySanXuat.Current as MaySanXuat;
            if (current != null)
            {
                btnSua.Enabled = btnXoa.Enabled = true;
            }
            else
            {
                btnSua.Enabled = btnXoa.Enabled = false;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new DuLieu.FormQuanLyMaySanXuat_ChiTiet(cmbCongDoanSanXuat.SelectedItem as CongDoanSanXuat);
            frm.OnChanged += (o, t) =>
            {
                LoadGrid();
            };
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var current = bsMaySanXuat.Current as MaySanXuat;
            if (current != null)
            {
                var frm = new DuLieu.FormQuanLyMaySanXuat_ChiTiet(cmbCongDoanSanXuat.SelectedItem as CongDoanSanXuat, current);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    LoadGrid();
            }
        }

        private void cmbDonViPhuTrach_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadGrid();
            }
        }

        private void gridData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (bsMaySanXuat.Count > 0 && e.RowIndex >= 0 && e.RowIndex < bsMaySanXuat.Count)
                btnSua.PerformClick();
        }

        private void cmbCongDoanSanXuat_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadGrid();
            }
        }
    }
}
